package Java.Lock.Callable;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;


//线程创建的方法


//比较两个接口的区别
//实现runnable接口
class Mythread1 implements Runnable {

    @Override
    public void run() {

    }
}


//实现callable接口
class Mythread2 implements Callable {

    @Override
    public Object call() throws Exception {
        System.out.println(Thread.currentThread().getName() + " come  in callable");
        return 200;
    }
}


public class Demo1 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //runnale接口创建线程
        new Thread(new Mythread1(), "AA").start();

        //callable接口,报错
//        new Thread((new Mythread2(),"BB")).start();

        //FutureTask
        FutureTask<Integer> futureTask1 = new FutureTask<>(new Mythread2());

        //lambda表达式
        FutureTask<Integer> futureTask2 = new FutureTask<>(() -> {
            System.out.println(Thread.currentThread().getName() + " come  in callable");
            return 1024;
        });

        //创建一个线程
        new Thread(futureTask2, "lucy").start();

        new Thread(futureTask1, "mary").start();


//        while (!futureTask2.isDone()){
//            System.out.println("wait!");
//        }


        //调用futuretask的get方法
        System.out.println(futureTask2.get());

        System.out.println(futureTask1.get());

        System.out.println(Thread.currentThread().getName() + " come over ");


        /**future原来  未来任务
         老师上课口渴了，叫同学买水，继续上课，同学买回来直接喝

         汇总一次
         **/

    }
}
